package com.qyhl.webtv.module_live.teletext.program;

import android.app.Activity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.webtv.commonlib.entity.live.ProgramDetailBean;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.utils.Event;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import razerdp.basepopup.BasePopupWindow;

/**
 * Created by qyhl on 2018/1/7.
 * 音频节目单
 */

public class RadioMenuPop extends BasePopupWindow implements LiveRadioInterface {
    private View popupView;
    private RecyclerView mRecyclerView;
    private RecyclerView recyclerView;
    private ImageView cancelBtn;
    private LoadingLayout loadMask;

    private ProgramDetailBean mBean;

    private com.zhy.adapter.recyclerview.CommonAdapter mAdapter;

    private com.zhy.adapter.recyclerview.CommonAdapter commonAdapter;

    private List<ProgramDetailBean.ProgramNoticeList> mDataList = new ArrayList<>();

    private List<ProgramDetailBean.ProgramNoticeList.Program> mMenuList = new ArrayList<>();

    private EmptyWrapper emptyWrapper;

    private int mWeekNum, mMenuNum;


    public RadioMenuPop(Activity context, ProgramDetailBean programDetailBean, int weekNum, int menuNum) {
        super(context);
        mBean = programDetailBean;
        mWeekNum = weekNum;
        mMenuNum = menuNum;
        bindEvent(context);
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(200, 0, 500);
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.live_pop_radio_menu, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }


    private void bindEvent(final Activity context) {
        mRecyclerView = (RecyclerView) popupView.findViewById(R.id.mRecyclerView);
        recyclerView = (RecyclerView) popupView.findViewById(R.id.recyclerView);
        cancelBtn = (ImageView) popupView.findViewById(R.id.cancel);
        loadMask = (LoadingLayout) popupView.findViewById(R.id.load_mask);
        loadMask.setStatus(LoadingLayout.Loading);

        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(commonAdapter = new com.zhy.adapter.recyclerview.CommonAdapter<ProgramDetailBean.ProgramNoticeList>(context, R.layout.live_item_program_menulist, mDataList) {
            @Override
            protected void convert(com.zhy.adapter.recyclerview.base.ViewHolder holder, ProgramDetailBean.ProgramNoticeList o, int position) {
                TextView week = holder.getView(R.id.week);
                TextView date = holder.getView(R.id.date);

                if (o.isSelected()) {
                    week.setTextColor(context.getResources().getColor(R.color.global_base));
                    date.setTextColor(context.getResources().getColor(R.color.global_base));
                } else {
                    week.setTextColor(context.getResources().getColor(R.color.global_black_lv1));
                    date.setTextColor(context.getResources().getColor(R.color.global_gray_lv2));
                }

                if (o.getIsLive() == 1) {
                    week.setText("今天");
                    date.setText(o.getsDate());
                } else {
                    week.setText(o.getsWeek());
                    date.setText(o.getsDate());
                }
            }

        });

        //设置布局管理器
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(context);
        linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager2);

        mAdapter = new com.zhy.adapter.recyclerview.CommonAdapter<ProgramDetailBean.ProgramNoticeList.Program>(context, R.layout.live_item_live_menu, mMenuList) {
            @Override
            protected void convert(ViewHolder viewHolder, ProgramDetailBean.ProgramNoticeList.Program item, int position) {
                TextView title = viewHolder.getView(R.id.title);
                ImageView status = viewHolder.getView(R.id.status);
                ImageView status_dot = viewHolder.getView(R.id.status_dot);
                TextView time = viewHolder.getView(R.id.time);
                ImageView line = viewHolder.getView(R.id.line);
                line.setVisibility(View.GONE);
                status_dot.setVisibility(View.GONE);
                //选中状态
                if (item.isSelected()) {
                    status.setVisibility(View.VISIBLE);
                    title.setTextColor(context.getResources().getColor(R.color.global_base));

                    Glide.with(context)
                            .load(R.drawable.status_playing)
                            .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE))
                            .into(status);

                    time.setTextColor(context.getResources().getColor(R.color.global_base));
                } else {
                    if (item.getStatus() == 1) {
                        status.setVisibility(View.VISIBLE);
                        title.setTextColor(context.getResources().getColor(R.color.global_black_lv1));

                        Glide.with(context)
                                .load(R.drawable.status_radio_review)
                                .into(status);

                        time.setTextColor(context.getResources().getColor(R.color.gray_status_menu));
                    } else if (item.getStatus() == 0) {
                        status.setVisibility(View.VISIBLE);
                        title.setTextColor(context.getResources().getColor(R.color.global_black_lv1));

                        Glide.with(context)
                                .load(R.drawable.status_live)
                                .into(status);

                        time.setTextColor(context.getResources().getColor(R.color.gray_status_menu));
                    } else {
                        title.setTextColor(context.getResources().getColor(R.color.gray_status_menu));

                        status.setVisibility(View.GONE);

                        time.setTextColor(context.getResources().getColor(R.color.gray_status_menu));
                    }
                }

                time.setText(item.getsTime());

                title.setText(item.getName());

            }
        };

        emptyWrapper = new EmptyWrapper(mAdapter);
        emptyWrapper.setEmptyView(R.layout.live_layout_list_emptyview);
        mRecyclerView.setAdapter(emptyWrapper);

        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (mMenuList.get(position).getStatus() == 2) {
                    Toasty.normal(context, "尚未开始，敬请期待...").show();
                } else {
                    mMenuNum = position;

//                    for (int i = 0; i < mDataList.size(); i++) {
//                        for (int j = 0; j < mDataList.get(i).getProgram().size(); j++) {
//                            if (mWeekNum == i && mMenuNum == j) {
//                                mDataList.get(i).getProgram().get(j).setSelected(true);
//                            } else {
//                                mDataList.get(i).getProgram().get(j).setSelected(false);
//                            }
//                        }
//                    }
//                    mMenuList.clear();
//                    mMenuList.addAll(mDataList.get(mWeekNum).getProgram());
//
//                    mAdapter.notifyDataSetChanged();
//                    emptyWrapper.notifyDataSetChanged();
                    BusFactory.getBus().post(new Event.RadioEvent(mWeekNum, mMenuNum));
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                mWeekNum = position;
                for (int i = 0; i < mBean.getProgramDay().size(); i++) {
                    if (i == mWeekNum) {
                        mBean.getProgramDay().get(i).setSelected(true);
                    } else {
                        mBean.getProgramDay().get(i).setSelected(false);
                    }
                }

                mDataList.clear();
                mDataList.addAll(mBean.getProgramDay());

                mMenuList.clear();
                mMenuList.addAll(mDataList.get(mWeekNum).getProgram());

                commonAdapter.notifyDataSetChanged();
                mAdapter.notifyDataSetChanged();
                emptyWrapper.notifyDataSetChanged();

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        setData(mWeekNum, mMenuNum);
    }

    /**
     * 设置数据
     */
    private void setData(int weekNum, int menuNum) {
        //是否有节目单
        if (mBean.getProgram().getIsNotice() == 0) {
            loadMask.setStatus(LoadingLayout.Empty);
            loadMask.setEmptyText("小编正在准备内容");
        } else {
            if (mBean.getProgramDay() != null && mBean.getProgramDay().size() > 0) {
                loadMask.setStatus(LoadingLayout.Success);
                for (int i = 0; i < mBean.getProgramDay().size(); i++) {
                    if (i == weekNum) {
                        mBean.getProgramDay().get(i).setSelected(true);
                    } else {
                        mBean.getProgramDay().get(i).setSelected(false);
                    }
                    for (int j = 0; j < mBean.getProgramDay().get(i).getProgram().size(); j++) {
                        if (i == weekNum && j == menuNum) {
                            mBean.getProgramDay().get(i).getProgram().get(j).setSelected(true);
                        } else {
                            mBean.getProgramDay().get(i).getProgram().get(j).setSelected(false);
                        }
                    }
                }

                mDataList.clear();
                mDataList.addAll(mBean.getProgramDay());

                mMenuList.clear();
                mMenuList.addAll(mDataList.get(weekNum).getProgram());

                mAdapter.notifyDataSetChanged();
                commonAdapter.notifyDataSetChanged();
                emptyWrapper.notifyDataSetChanged();
            } else {
                loadMask.setStatus(LoadingLayout.Empty);
                loadMask.setEmptyText("小编正在准备内容");
            }
        }

        recyclerView.scrollToPosition(mWeekNum);
    }

    @Override
    public void refresh(int weekNum, int menuNum) {
        setData(weekNum, menuNum);
    }

    /**
     * 刷新数据源
     *
     * @param bean
     */
    @Override
    public void refreshData(ProgramDetailBean bean) {
        mBean = bean;
    }
}
